package com.demo.common.exceptions;

import com.demo.common.vo.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * 统一异常处理器
 */
@Slf4j
@ResponseStatus(HttpStatus.ACCEPTED)
@RestControllerAdvice
public class GlobalExceptionHandler {

    /**
     * 自定义业务异常
     * @param e
     * @return
     */
    @ExceptionHandler(ServiceException.class)
    public Result defaultErrorHandler(ServiceException e) {
        log.error("业务异常",e);
        return Result.error(5000,e.getMessage());
    }

    /**
     * 一般异常
     * @param e
     * @return
     */
    @ExceptionHandler(Exception.class)
    public Result defaultErrorHandler(Exception e) {
        log.error("程序异常",e);
        return Result.error(5000,"服务器开小差了");
    }

}
